package com.ticket.dao;

import com.ticket.entity.Order;
import com.ticket.util.FileUtil;

import java.util.HashMap;
import java.util.Map;

public class OrderDao {
    FileUtil fileUtil = new FileUtil();

    /**
     * 将文件中存储的字符串解析为订单对象
     * @param orderString
     * @return
     */
    public Map<String, Order> stringToMap(String orderString){
        Map<String,Order> orderMap = new HashMap<>();
        String [] orderArray = orderString.split("\n");
        for( String orderStr : orderArray){
            Order order = new Order();
            String [] orderInfo = orderStr.split("\t\t\t");
            order.setOrderId(orderInfo[0]);
            order.setUsername(orderInfo[1]);
            order.setTicketId(Integer.valueOf(orderInfo[2]));
            order.setStartStation(orderInfo[3]);
            order.setEndStation(orderInfo[4]);
            orderMap.put(order.getOrderId(),order);
        }
        return orderMap;
    }

    /**
     * 将订单对象转化为文件所存字符串
     * @param orderMap
     * @return
     */
    public String mapToString(Map<String,Order> orderMap){
        StringBuffer orderInfo = new StringBuffer();
        for (String key : orderMap.keySet()){
            Order order = orderMap.get(key);
            orderInfo.append(order.getOrderId() + "\t\t\t");
            orderInfo.append(order.getUsername() + "\t\t\t");
            orderInfo.append(order.getTicketId() + "\t\t\t");
            orderInfo.append(order.getStartStation() + "\t\t\t");
            orderInfo.append(order.getEndStation() + "\n");
        }
        return orderInfo.toString().trim();
    }

    /**
     * 查询所有的票
     * @return
     */
    private Map<String, Order> getAllOrders(){
        String orderString = fileUtil.getContent("order.txt");
        if(orderString != null && !orderString.isEmpty()){
            return this.stringToMap(orderString);
        }else {
            return null;
        }
    }
    /**
     * 保存所有有票
     * @param orderMap
     */
    private void saveAllOrder(Map<String, Order> orderMap){
        if(orderMap != null){
            String orderString = this.mapToString(orderMap);
            fileUtil.saveContent("order.txt",orderString);
        }
    }
    /**
     * 根据用户名查询票
     * @param username
     * @return
     */
    public Map<String,Order> getOrderByUsername(String username){
        Map<String,Order> orderMap = this.getAllOrders();
        Map<String,Order> resultMap = new HashMap<>();
        if(orderMap != null){
            for (String orderId : orderMap.keySet()){
                String orderUser = orderMap.get(orderId).getUsername();
                if(orderUser.equals(username)){
                    resultMap.put(orderId,orderMap.get(orderId));
                }
            }
        }
        return resultMap;
    }
    /**
     * 保存已购票
     * @param order
     */
    public void saveOrder(Order order){
        Map<String,Order> orderMap = this.getAllOrders();
        if(orderMap == null)
            orderMap = new HashMap<>();
        orderMap.put(order.getOrderId(),order);
        this.saveAllOrder(orderMap);
    }

    /**
     * 根据票号退票
     * @param orderId
     * @return
     */
    public boolean deleteOrder(String orderId){
        Map<String,Order> orderMap = this.getAllOrders();
        if(orderMap != null && orderMap.containsKey(orderId)){
            orderMap.remove(orderId);
            this.saveAllOrder(orderMap);
            return true;
        }else
            return false;
    }
    public Order getOrderById(String orderId){
        Map<String,Order> orderMap = this.getAllOrders();
        return orderMap.get(orderId);
    }


}
